package com.gzhuxj201.controller;

import com.gzhuxj201.entity.PageBean;
import com.gzhuxj201.entity.Result;
import com.gzhuxj201.entity.StuFile;
import com.gzhuxj201.entity.Teacher;
import com.gzhuxj201.service.StuFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/stuFiles")
@Slf4j
public class StuFileController {
    @Autowired
    private StuFileService stuFileService;

    @GetMapping
    public Result page(Integer page, Integer pageSize,Integer homeWorkId,String sno,String studentName, HttpSession session){
        Teacher teacher = (Teacher)session.getAttribute("user");
        String tno = teacher.getTno();
        PageBean pageBean=stuFileService.page(page,pageSize,tno,homeWorkId,sno,studentName);
        return Result.success(pageBean);
    }

    @GetMapping("{id}/{sno}")
    public Result selectByHomeWorkIdAndSno(@PathVariable Integer id,@PathVariable String sno, HttpSession session){

        StuFile stuFile=stuFileService.selectByHomeWorkIdAndSno(id,sno);
        if(stuFile==null){
            return Result.error("记得按时提交作业哦");
        }else {
            log.info(stuFile.toString());
            return Result.success(stuFile);
        }


    }

    @PostMapping
    public Result add(@RequestBody StuFile stuFile){
        int res=stuFileService.add(stuFile);
        return Result.success();
    }

}
